﻿package game.ui {
	import game.debug.Debug;
	import game.objects.GameObject;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.AntiAliasType;
	import flash.text.TextFieldAutoSize;
	import flash.events.Event;
	
	public class Text extends GameObject {
		
		private var TF:TextField;
		
		public static const CENTER:String	= TextFieldAutoSize.CENTER;
		public static const LEFT:String		= TextFieldAutoSize.LEFT;
		public static const RIGHT:String	= TextFieldAutoSize.RIGHT;
		
		public function Text():void {
			
			TF						= new TextField;
			TF.defaultTextFormat	= config.defaultTextFormat;
			TF.selectable			= false;
			TF.mouseEnabled			= false;
			TF.embedFonts			= true;
			TF.antiAliasType		= AntiAliasType.ADVANCED;
			TF.autoSize				= TextFieldAutoSize.LEFT;
			
			TF.addEventListener	( Event.CHANGE, changed );
			
			var test:Boolean = false;
			//проверяем дебаг ли
			for each (var d:String in config.debug) {
				if (d==Debug.DYNAMIC_OBJECTS) {
					test = true;
					break;
				}
			}
			
			if (test) {
				TF.border		= true;
				TF.borderColor	= 0xff0000;
			}
			
			addChild(TF);
			
		}
		
		public function set align( value:String ):void {
			TF.autoSize = value;
		}
		
		public function set multiline(value:Boolean):void {
			TF.multiline = value;
			TF.wordWrap = value;
		}
		
		public override function set height(value:Number):void {
			TF.height = value;
		}
		
		public override function set width(value:Number):void {
			TF.width = value;
		}
		
		public function set (str:String, params:Object = null, multi:Boolean = true):void {
			try {
				lang.set(TF, str, params, multi);
			} catch (e:Error) {
				trace (e);
			}
		}
		
		public function setFormat(params:String, value:*):void {
			var tf:TextFormat = TF.defaultTextFormat;
			tf[params] = value;
			TF.defaultTextFormat = tf;
			TF.setTextFormat(tf);
		}
		
		private function changed(event:Event):void {
			this.dispatchEvent( new Event ( Event.CHANGE ) );
		}
	}
}